import { Op } from 'sequelize';
import { db, models } from '../../db';

const {
  config_vip: ConfigVipModel,
  user: UserModel,
  user_log_assets: UserLogAssetsModel,
  user_hero: UserHeroModel,
  user_equip: UserEquipModel,
} = models;

export const vipCanGet = async (user_id: string) => {
  return new Promise(async (resolve, reject) => {
    try {
      // 拿到所有可获得的vip
      const vips = await ConfigVipModel.findAll({
        attributes: ['id'],
      });
      const vipLevels = vips.map(item => item.dataValues.id); // vip等级
      // 对比用户目前的vip
      const user = await UserModel.findOne({ where: { id: user_id } });
      if (!user) {
        return reject('用户不存在');
      }

      const { vip } = user.dataValues;

      const canGet: number[] = vipLevels.filter(item => item <= vip);

      const isGet = await UserLogAssetsModel.findAll({
        where: {
          user_id,
          reason_type: {
            [Op.like]: '%vip%',
          },
        },
      });
      const isGetNum: number[] = isGet.map(item =>
        parseInt(item.dataValues.reason_type.replace('vip', '')),
      );

      const res: number[] = canGet.filter(item => !isGetNum.includes(item));

      return resolve(res);
    } catch (error) {
      console.log(error);
    }
  });
};

export const vipReward = async (user_id: string, data: Record<string, any>) => {
  return new Promise(async (resolve, reject) => {
    const transaction = await db.transaction();
    try {
      const { vip } = data;

      if (!vip) {
        return reject();
      }

      const user = await UserModel.findOne({ where: { id: user_id } });
      if (!user) {
        return reject();
      }

      const { vip: userVip } = user.dataValues;

      if (userVip < vip) {
        return reject();
      }

      const get = await ConfigVipModel.findOne({ where: { id: vip } });
      if (!get) {
        return reject();
      }

      const { hero, equip } = get.dataValues;

      const res: Record<string, any> = [];

      if (hero) {
        const newHero = await UserHeroModel.create(
          {
            user_id,
            hero_id: hero,
            position_x: 0,
            position_y: 0,
          },
          {
            transaction,
          },
        );

        const { id: newHeroId } = newHero.dataValues;
        await UserLogAssetsModel.create(
          {
            user_id,
            type: 'get',
            assets_id: newHeroId,
            assets_type: 'hero',
            reason_type: `vip${vip}`,
            reason: '获得vip英雄',
          },
          {
            transaction,
          },
        );
        res.push({
          id: hero,
          count: 1,
        });
      }

      if (equip) {
        const newEquip = await UserEquipModel.create(
          {
            user_id,
            equip_id: equip,
          },
          {
            transaction,
          },
        );

        const { id: newEquipId } = newEquip.dataValues;
        await UserLogAssetsModel.create(
          {
            user_id,
            type: 'get',
            assets_id: newEquipId,
            assets_type: 'equip',
            reason_type: `vip${vip}`,
            reason: '获得vip装备',
          },
          {
            transaction,
          },
        );
        res.push({
          id: equip,
          count: 1,
        });
      }

      await transaction.commit();
      return resolve(res);
    } catch (error) {
      await transaction.rollback();
      console.log(error);
    }
  });
};
